<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//前台登录控制器
class LoginController extends Controller{
	protected $model;
	//构造函数
	public function __init(){
		 //实例化user模型
		 $this->model = new \Common\Model\User;
	}
	//登录
	public function index(){
		if(IS_POST){
		    //p(Q("post."));
    		//接收数据
    	    $user_name = Q('post.user_name');
			$password = Q('post.password','','md5');
			//判断用户名是否存在
			$model = new \Common\Model\User;
			//从数据库里获得用户数据
			$data = $model->where("user_name='{$user_name}'")->find();
    	    if(!$data){
    	    	View::error('用户名不正确');
    	    }
			//判断密码是否正确
			if($data['password'] != $password){
				View::error('密码不正确');
			}
			//登录成功
			$_SESSION['user_id']   = $data['user_id'];
			$_SESSION['user_name'] = $data['user_name'];
			View::success('登录成功',U('Index/index'));
		}  
       View::make();
	}
	//验证码
	public function code(){
		Code::num('4')->fontColor('#006699')->make();
	}
	//退出
	public function logout(){
		session_unset();
		session_destroy();
		//跳转到登录页
		go(U('Login/index'));
	}
	
}

 ?>